<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="赛项名称" prop="itemName">
        <el-input v-model="dataForm.itemName" placeholder="赛项名称"></el-input>
      </el-form-item>
      <el-form-item label="赛项类别" prop="itemClassify">
        <el-select v-model="dataForm.itemClassify" placeholder="赛项类别" clearable>
          <el-option :label="data.dictLabel" v-for="data in itemClassifyList" :key="data.id" :value="data.id">{{ data.dictLabel }}</el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="赛项分类" prop="itemType">
        <el-select v-model="dataForm.itemType" placeholder="赛项分类" clearable>
          <el-option :label="data.dictLabel" v-for="data in itemTypeList" :key="parseInt(data.id)" :value="parseInt(data.id)">{{ data.dictLabel }}</el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="老师的数量" prop="teacherNum">
        <el-input v-model="dataForm.teacherNum" style="width: 100px"></el-input>
        ~
        <el-input v-model="dataForm.teacherNumMax" style="width: 100px"></el-input>
      </el-form-item>
      <el-form-item label="学生的数量" prop="studentNum">
        <el-input v-model="dataForm.studentNum" style="width: 100px"></el-input>
        ~
        <el-input v-model="dataForm.studentNumMax" style="width: 100px"></el-input>
      </el-form-item>
      <el-form-item prop="isDraw" label="是否抽签">
        <el-radio-group v-model="dataForm.isDraw">
          <el-radio :label="0">否</el-radio>
          <el-radio :label="1">是</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item prop="isPromiss" label="参赛承诺书">
        <el-radio-group v-model="dataForm.isPromiss">
          <el-radio :label="0">否</el-radio>
          <el-radio :label="1">是</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item prop="score" label="成绩">
        <el-select v-model="scoreSelected" multiple placeholder="成绩">
          <el-option :label="data.dictLabel" v-for="data in scoreList" :key="data.dictValue" :value="data.dictValue">{{ data.dictLabel }}</el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number v-model="dataForm.sort" controls-position="right" :min="0" label="排序"></el-input-number>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, reactive } from "vue";
import baseService from "@/service/baseService";
import { getDictDataList } from "@/utils/utils";
import { useDebounce } from "@/utils/utils";
import store from "@/store";
export default defineComponent({
  setup() {
    return reactive({
      visible: false,
      dataForm: {
        id: "",
        itemName: "",
        itemClassify: "",
        itemType: "",
        teacherNum: "",
        teacherNumMax: "",
        studentNum: "",
        studentNumMax: "",
        isDraw: "",
        isPromiss: "",
        scores: "",
        remark: "",
        sort: ""
      },
      itemClassifyList: getDictDataList(store.state.dicts, "itemClassify"),
      itemTypeList: getDictDataList(store.state.dicts, "itemType"),
      scoreList: getDictDataList(store.state.dicts, "scores"),
      scoreSelected: [] as any[]
    });
  },
  computed: {
    dataRule() {
      return {
        itemName: [{ required: true, message: this.$t("validate.required"), trigger: "blur" }],
        itemClassify: [{ required: true, message: this.$t("validate.required"), trigger: "blur" }],
        itemType: [{ required: true, message: this.$t("validate.required"), trigger: "blur" }],
        teacherNum: [{ required: true, message: this.$t("validate.required"), trigger: "blur" }],
        teacherNumMax: [{ required: true, message: this.$t("validate.required"), trigger: "blur" }],
        studentNum: [{ required: true, message: this.$t("validate.required"), trigger: "blur" }],
        studentNumMax: [{ required: true, message: this.$t("validate.required"), trigger: "blur" }]
      };
    }
  },
  created() {
    this.dataFormSubmitHandle = useDebounce(this.dataFormSubmitHandle);
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataFormRef"].resetFields();
        if (this.dataForm.id) {
          this.scoreSelected = [];
          this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      baseService.get("/sp/spbasematchitem/" + this.dataForm.id).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.dataForm = res.data;
        this.scoreSelected = this.dataForm.scores.split(",");
        console.log(this.scoreSelected);
        this.$forceUpdate();
      });
    },
    // 表单提交
    dataFormSubmitHandle() {
      this.$refs["dataFormRef"].validate((valid: boolean) => {
        if (!valid) {
          return false;
        }
        this.dataForm.scores = this.scoreSelected.join(",");
        (!this.dataForm.id ? baseService.post : baseService.put)("/sp/spbasematchitem", this.dataForm).then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message({
            message: this.$t("prompt.success"),
            type: "success",
            duration: 500,
            onClose: () => {
              this.visible = false;
              this.$emit("refreshDataList");
            }
          });
        });
      });
    }
  }
});
</script>
